package com.walterweimod.auxiliary_attributes.capability.impl.base;

import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.util.INBTSerializable;
import net.minecraftforge.common.util.LazyOptional;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class CapabilityProviderBase<C extends INBTSerializable<CompoundTag>>
        implements ICapabilityProvider, INBTSerializable<CompoundTag> {
    private final C capabilityType;
    private final Capability<C> capability;
    private final LazyOptional<C> optional;

    public CapabilityProviderBase(C capabilityType, Capability<C> capability){
        this.capabilityType = capabilityType;
        this.capability = capability;
        this.optional = LazyOptional.of(() -> capabilityType);
    }

    @Override
    public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
        return cap == capability ? optional.cast() : LazyOptional.empty();
    }

    @Override
    public CompoundTag serializeNBT() {
        return capabilityType.serializeNBT();
    }

    @Override
    public void deserializeNBT(CompoundTag nbt) {
        capabilityType.deserializeNBT(nbt);
    }
}
